package com.cloudeasy.user.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloudeasy.api.util.EntityUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.user.PermissionVO;
import com.cloudeasy.user.field.PermissionField;
import com.cloudeasy.user.po.Permission;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

public class PermissionUtil {

    /**
     * 转换VO对象
     *
     * @param permission {@link Permission}
     * @return {@link PermissionVO}
     */
    private static PermissionVO convert(Permission permission) {
        PermissionVO permissionVO = new PermissionVO();
        BeanUtils.copyProperties(permission, permissionVO);
        return permissionVO;
    }

    /**
     * 转换VO对象
     *
     * @param permissions {@link List<Permission>}
     * @return {@link List<PermissionVO>}
     */
    public static List<PermissionVO> convert(List<Permission> permissions) {
        return permissions.stream().map(PermissionUtil::convert).collect(Collectors.toList());
    }

    /**
     * 转换VO对象
     *
     * @param permissionIPage {@link IPage<Permission>}
     * @return {@link PageResult<PermissionVO>}
     */
    public static PageResult<PermissionVO> convert(IPage<Permission> permissionIPage) {
        PageResult<PermissionVO> pageResult = new PageResult<>();
        pageResult.setTotal(permissionIPage.getTotal())
                .setPages(permissionIPage.getPages())
                .setRecords(permissionIPage.convert(PermissionUtil::convert).getRecords());
        return pageResult;
    }
}
